<?php

/**
 * Capital Direct Lending (http://www.philbsd.com/cdl)
 *
 * Copyright (C) 2008 Philippine Business Solution Developers <http://www.philbsd.com>
 * All rights reserved.
 *
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@philbsd.com so we can send you a copy immediately.
 *
 * @copyright  (c) 2008 Philippine Business Solution Developers. (http://www.philbsd.com)
 * @license    http://www.gnu.org/licenses/gpl-3.0.txt
 * @version    $Id: collateral.php 161 2008-04-13 07:25:10Z artiskool $
 */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Collateral extends SB_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->_data['menuLoan'] = true;
	}

	public function index()
	{
		//$this->input->post();
		//$this->load->library('calendar');
		//echo $this->calendar->generate();
		$this->render('collateral/index.shtml');
	}

	public function view()
	{
		$this->render('collateral/view.shtml');
	}

	public function add()
	{
		$this->render('collateral/add.shtml');
	}

        public function popadd($borrower_id)
        {
		$this->_data['borrower_id'] = $borrower_id;
		if ($this->input->post('borrower_id')) {
			$this->_data['borrower_id'] = $this->input->post('borrower_id');
		}
                $this->load->helper(array('form', 'url'));
                $this->load->library('validation');

                if (isset($_POST['Collateral/item_description'])) {

                        $failedUpload = false;
                        $photoDir = PHOTO_DIR . DS . 'collaterals';

                        $uploadConfig['allowed_types'] = 'gif|jpg|png';
                        $uploadConfig['max_size']       = '10240';
                        $uploadConfig['max_width']  = '4000';
                        $uploadConfig['max_height']  = '4000';
                        $this->load->library('upload');

                        $imgConfig['create_thumb'] = TRUE;
                        $imgConfig['maintain_ratio'] = TRUE;
                        $imgConfig['width'] = 96;
                        $imgConfig['height'] = 96;
                        $this->load->library('image_lib');

                        $rules['Collateral/item_description'] = "required";
                        $rules['Collateral/model'] = "required";
                        $rules['Collateral/serial'] = "required";
                        $rules['Collateral/purchased_date'] = "required";
                        $rules['Collateral/status'] = "required";
                        $this->validation->set_rules($rules);

                        $fields['Collateral/item_description'] = 'Item Description';
                        $fields['Collateral/model'] = 'Model#';
                        $fields['Collateral/serial'] = 'Serial#';
                        $fields['Collateral/purchased_date'] = 'Date of Purchase';
                        $fields['Collateral/status'] = 'Collateral Status';
                        $this->validation->set_fields($fields);

                        if ($this->validation->run()) {
                                $this->db->trans_begin();

                                // adding collateral
                                $data = $this->_swordPost['Collateral'];
                                $this->db->insert('collaterals', $data);
                                $collateralId = $this->db->insert_id();

                                // uploading collateral's photo if exists
                                if (isset($_FILES['collateral_photo']['name'])) {
                                        $uploadConfig['upload_path'] = $photoDir . DS;
                                        $this->upload->initialize($uploadConfig);
                                        if (!$this->upload->do_upload('collateral_photo')) {
                                                $failedUpload = true;
                                                $errMsg = "\n<br />Failed to upload collateral's photo. " . $this->upload->display_errors('', '');
                                                if (isset($this->_data['msg'])) {
                                                        $this->_data['msg'] .= $errMsg;
                                                } else {


                                                        $this->_data['msg'] = $errMsg;
                                                }
                                        } else {
                                                $photoExt = pathinfo($_FILES['collateral_photo']['name'], PATHINFO_EXTENSION);
                                                $photoDestination = $photoDir . DS . "{$collateralId}.{$photoExt}";
                                                $photoThumbDestination = $photoDir . DS . "{$collateralId}_thumb.{$photoExt}";
                                                @rename($uploadConfig['upload_path'] . $_FILES['collateral_photo']['name'], $photoDestination);
                                                $imgConfig['source_image'] = $photoDestination;
                                                $this->image_lib->initialize($imgConfig);
                                                if (!$this->image_lib->resize()) {
                                                        $failedUpload = true;
                                                        $errMsg = "\n<br />{$imgConfig['source_image']}Failed to resize collateral's photo. " . $this->image_lib->display_errors('', '');
                                                        if (isset($this->_data['msg'])) {
                                                                $this->_data['msg'] .= "\n<br />{$errMsg}";
                                                        } else {
                                                                $this->_data['msg'] = $errMsg;
                                                        }
                                                }
                                        }
                                }


                                if ($this->db->trans_status() === FALSE || $failedUpload) {
                                        if (isset($photoDestination)) {
                                                $this->_unlinkIfExists($photoDestination);
                                        }
                                        if (isset($photoThumbDestination)) {
                                                $this->_unlinkIfExists($photoThumbDestination);
                                        }

                                        $this->db->trans_rollback();
                                        $errMsg = "Failed to add!";
                                        if (isset($this->_data['msg'])) {
                                                $this->_data['msg'] .= $errMsg;
                                        } else {
                                                $this->_data['msg'] = $errMsg;
                                        }
                                } else {
                                        $this->db->trans_commit();
                                        $this->_data['success_msg'] = "Successfully saved.";
                                        $this->_data['my_id'] = $collateralId;
                                }

                        }
                }

                $this->render('collateral/popadd.shtml', array(), 'popup.shtml');
        }

        function popdel($collateral_id)
        {
                $this->_data['collateral_id'] = $collateral_id;
                if ($this->input->post('collateral_id')) {
                        $this->db->delete('collaterals', array('id' => $this->input->post('collateral_id')));
                        $this->_data['success_msg'] = "Successfully deleted.";
                }
                $this->render('collateral/popdel.shtml', array(), 'popup.shtml');
        }
}
